<?php

/*
    picopress - A lightweighed online publishing system
    Copyright (C) 2010  Martin Burkert <martin@burkert.de>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
    
*/

class Cache {

    public $node;
    public $cache_file;
    public $level = 0;
    
    public function set_node ($node) {
        $this->node = $node;
        
        if(!is_writable(CACHE_DIR)){
            die ('Error: Directory '.CACHE_DIR.' is not writable. Please set permissions. (Cache)');
        }
        $this->cache_file = CACHE_DIR.'/_'.base64_url_encode($node).'.'.$this->get_level().CACHE_EXTENSION;
    }
    
    public function is_available() {
        return is_file($this->get_cache_file());
    }
    
    public function get_cache_file(){
        return $this->cache_file;
    }

    public function get_level(){
        return $this->level;
    }
    
    
    public function save ($contents) {
    
        $filename = $this->get_cache_file();

        if($handle = fopen($filename, 'w+')){
            if (!fwrite($handle, $contents)) {
                die('Error: Cannot write to file '.$filename.'. (Cache)');
            }           
        }else{
            die ('Error: Cannot open '.$filename.' for writing. (Cache)');
        }           
    }

}




?>